package com.nervenets.web.controller.handler.admin;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.enumeration.*;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

public class CheckStatusHandler extends SessionHandler<TokenParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(TokenParams params, HttpServletRequest request) throws Exception {
        Model model = new Model();

        HashMap<String, Object> objects = new HashMap<>();
        objects.put("authStatus", AuthStatus.checking);
        model.setAuthCount(userDao.getCountOfByHql("from User where authStatus = :authStatus", objects).intValue());
        model.setBankCount(userDao.getCountOfByHql("from Bank where authStatus = :authStatus", objects).intValue());

        objects = new HashMap<>();
        model.setMovieCount(userDao.getCountOfByHql("from Movie where checkInfo like '%\"status\":\"checking\"%'", objects).intValue());

        objects.put("orderStatus", OrderStatus.paid);
        model.setOrderDeliveryCount(userDao.getCountOfByHql("from Order where status = :orderStatus", objects).intValue());
        objects.put("orderStatus", OrderStatus.tailConfirm);
        model.setOrderDeliveryCount(userDao.getCountOfByHql("from Order where status = :orderStatus", objects).intValue());

        objects = new HashMap<>();
        objects.put("wealthType", WealthType.money);
        objects.put("flowStatus", FlowStatus.handling);
        model.setWithdrawCount(userDao.getCountOfByHql("from WealthFlow where wealthType = :wealthType and flowStatus = :flowStatus", objects).intValue());
        return successMessage(model);
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }

    @Data
    class Model {
        private int authCount;
        private int bankCount;
        private int movieCount;
        private int orderDeliveryCount;
        private int orderTailConfirmCount;
        private int withdrawCount;
    }
}
